﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Data;

namespace WfActivityLibrary.common
{
    public static class ListViewControlUtil
    {
        public const string VIEW_HEAD = "viewHead";   //头部
        public const string VIEW_DATA = "viewData"; //数据

        //初始化listview
        public static void iniToDetail(ListView listView, bool checkAble)
        {
            // 默认视图模式为Details
            listView.View = System.Windows.Forms.View.Details;
            listView.MultiSelect = false;            //不允许多项选择
            listView.LabelEdit = false;             // 不允许编辑项目文本
            listView.AllowColumnReorder = true;     // 允许用户重新安排各列
            listView.CheckBoxes = checkAble;        // 显示复选框
            listView.FullRowSelect = true;          // 在选取时选取项目和子项
            listView.GridLines = true;              // 显示网格线
            //listView.Sorting = SortOrder.Ascending; // 按降序排列项目
            listView.Clear();
        }

        /// <summary>
        /// 填充listview
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="data"></param>
        public static void fillListView(ListView listView, Dictionary<string, Object> data)
        {
            ListViewItem item;
            ListViewItem.ListViewSubItem subItem;

            if (data.ContainsKey(VIEW_HEAD) && data[VIEW_HEAD] != null)
            {
                string[,] heads = (string[,])data[VIEW_HEAD];
                for (int i = 0; i < heads.GetLength(0); i++)
                {
                    listView.Columns.Add(heads[i, 0], Convert.ToInt32(heads[i, 1]));
                }
            }
            if (data.ContainsKey(VIEW_DATA) && data[VIEW_DATA] != null)
            {
                string[] tmpData;
                string[][] datas = (string[][])data[VIEW_DATA];
                for (int i = 0; i < datas.GetLength(0); i++)
                {
                    item = listView.Items.Add((i + 1).ToString());
                    tmpData = datas[i];
                    item.Tag = tmpData[0];
                    for (int j = 1; j < tmpData.GetLength(0); j++)
                    {
                        //MessageBox.Show(tmpData[j]);
                        subItem = item.SubItems.Add(tmpData[j]);
                        // bb.BackColor = #786143;
                    }
                }
            }
        }

        /// <summary>
        ///以datatable数据源 填充listview
        /// </summary>
        /// <param name="dt">datatable表</param>
        /// <param name="lv">listview</param>
        /// <param name="Is_dv">是否要以dataview显示</param>
        /// <returns>DataView</returns>
        public static DataView fillListView(DataTable dt, ListView lv, bool Is_dv)
        {
            DataView dv = new DataView();
            MessageBox.Show(dt.Rows.Count.ToString());
            if (dt.Rows.Count == 0)
                return null;
            else
            {
                if (Is_dv)
                {
                    dv.Table = dt;
                    DataRowView drv;
                    for (int i = 0; i < dv.Count; i++)
                    {
                        drv = dv[i];
                        lv.Items.Add(drv[0].ToString().Trim());
                        for (int j = 1; j < lv.Columns.Count; j++)
                            lv.Items[j].SubItems.Add(drv[j].ToString().Trim());

                    }
                }
                else
                {
                    dv = null;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        lv.Items.Add(dr[0].ToString().Trim());
                        for (int j = 1; j < dt.Columns.Count; j++)
                        {
                            lv.Items[i].SubItems.Add(dr[j].ToString().Trim());
                        }
                    }
                }
            }
            return dv;
        }

    }
}
